package fr.ecp.sio.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class User {

	private String email;
	private String password;
	
	private final String jdbcURL = "jdbc:postgresql://localhost/CloudAtlasDB?user=clouddba&password=password";

	public User() {
		super();
	}

	public User(String email, String password) {
		super();
		this.email = email;
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [email=" + email + ", password=" + password + "]";
	}

	public boolean exists() {
		// TODO Auto-generated method stub

		// dans ce code on verifie que le "user" existe dans la BD
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection(jdbcURL);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM Account WHERE mail='"+this.email+"'");
			while(rs.next()){
				String email = rs.getString("mail");
				if(email.equals(this.email)){
					System.out.println("user already exists");
					return true;	
				}
			}
			System.out.println("user doesn't exist");
			return false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public boolean isCompany() {
		// TODO Auto-generated method stub

		// dans cette fonction on doit verifier s'il s'aggit d'un boite ou d'un
		// user normal
		return false;
	}

	public void createOnDB() {
		// TODO Auto-generated method stub
		//ici on fait l'INSERT dans la BDD
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection(jdbcURL);
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("INSERT INTO Account (mail,password,userType) VALUES ('"+this.email+"','"+this.password+"','standard')");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public boolean isCorrect() {
		// TODO Auto-generated method stub
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection(jdbcURL);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM Account WHERE mail='"+this.email+"'");
			while(rs.next()){
				String password = rs.getString("password");
				if(password.equals(this.password)){
					return true;
				}
			}
			return false;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}
	
	

}
